﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 人员管理
{
    public partial class Form1 : Form
    {
        PersonService personService;

        public Form1()
        {
            InitializeComponent();
            personService = new PersonService();
            ////设置数据表格为只读
            //dataGridView1.ReadOnly = false;
            ////不允许添加行
            //dataGridView1.AllowUserToAddRows = false;
            ////背景为白色
            //dataGridView1.BackgroundColor = Color.White;
            ////只允许选中单行
            //dataGridView1.MultiSelect = false;
            ////整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        DataTable initDt(List<Person> persons)
        {
            DataTable dt = new DataTable();

            //给表定义列
            DataColumn id = new DataColumn("编号", typeof(int));
            DataColumn name = new DataColumn("姓名", typeof(string));
            DataColumn age = new DataColumn("年龄", typeof(int));
            DataColumn sex = new DataColumn("性别", typeof(string));
            DataColumn addr = new DataColumn("地址", typeof(string));

            //把列放到表里面
            //*****用数组方式批量添加******
            dt.Columns.AddRange(
                new DataColumn[] { id, name, age, sex, addr }
            );
            if (persons != null && persons.Count > 0)
            {
                foreach (Person p in persons)
                {
                    dt.Rows.Add(p.ID, p.Name, p.Age, p.Sex, p.Addr);
                }
            }
            return dt;
        }

        /// <summary>
        /// 窗体onload
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void initTable(object sender, EventArgs e)
        {
            List<Person> persons = personService.findAll();
            DataTable dt = initDt(persons);
            // 设置表格控件的DataSource属性
            dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoAdd(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Text = "新建人员";
            form2.Owner = this;
            form2.StartPosition = FormStartPosition.CenterParent;
            form2.refresh += RefreshForm; // 父窗口加入委托
            form2.person = new Person();
            form2.ShowDialog();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        public void RefreshForm()// 父窗口 定义 委托具体逻辑
        {
            List<Person> persons = personService.findAll();
            DataTable dt = initDt(persons);
            // 设置表格控件的DataSource属性
            dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoEdit(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = 
                dataGridView1.SelectedRows;
            int count = selectedRows.Count;
            if(count > 0)
            {
                Form2 form2 = new Form2();
                form2.Text = "修改人员";
                Person person = SelectRow2Person();
                form2.person = person;
                form2.initData(person);
                form2.refresh += RefreshForm; // 父窗口加入委托
                form2.ShowDialog();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoDel(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
            foreach(DataGridViewRow item in selectedRows)
            {
                int id = Convert.ToInt32(item.Cells[0].Value);
                personService.deletePerson(id);
                RefreshForm();
            }
        }

        private Person SelectRow2Person()
        {
            Person p = new Person();
            DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
            int count = selectedRows.Count;
            if(count >=1)
            {
                DataGridViewRow dataGridViewRow = selectedRows[0];
                int id = Convert.ToInt32(dataGridViewRow.Cells[0].Value);
                string name = Convert.ToString(dataGridViewRow.Cells[1].Value);
                int age = Convert.ToInt32(dataGridViewRow.Cells[2].Value);
                string sex = Convert.ToString(dataGridViewRow.Cells[3].Value);
                string addr = Convert.ToString(dataGridViewRow.Cells[4].Value);

                p.ID = id;
                p.Name = name;
                p.Age = age;
                p.Sex = sex;
                p.Addr = addr;
            }
            return p;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoSearch(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            if(name != null && name.Length >0)
            {
                List<Person> persons = personService.findPersonsByName(name);
                DataTable dt = initDt(persons);
                dataGridView1.DataSource = dt;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {

            DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
            Console.WriteLine(selectedRows.Count + "");
            DataGridViewSelectedCellCollection selectedCells = dataGridView1.SelectedCells;
            Console.WriteLine(selectedRows.Count + "");
            int columnIndex = selectedCells[0].ColumnIndex;
            Console.WriteLine(columnIndex + "");

            //dataGridView1.BeginEdit(false);
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Text = "新建人员";
            form2.Owner = this;
            form2.StartPosition = FormStartPosition.CenterParent;
            form2.refresh += RefreshForm; // 父窗口加入委托
            form2.person = new Person();
            form2.ShowDialog();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
            int count = selectedRows.Count;
            if (count > 0)
            {
                Form2 form2 = new Form2();
                form2.Text = "修改人员";
                Person person = SelectRow2Person();
                form2.person = person;
                form2.initData(person);
                form2.refresh += RefreshForm; // 父窗口加入委托
                form2.ShowDialog();
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
            foreach (DataGridViewRow item in selectedRows)
            {
                int id = Convert.ToInt32(item.Cells[0].Value);
                personService.deletePerson(id);
                RefreshForm();
            }
        }
    }
}
